package com.bocloud.cmp.service.utils;

import org.slf4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.resource.Server;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.ssh.SSH;

public class ServerHelper {

	public static BsmResult fromSSH(String username, String password, String ip, Integer port, Logger logger) {
		SSH ssh = null;
		if (null == port) {
			ssh = new SSH(ip, username, password);
		} else {
			ssh = new SSH(ip, username, password, port);
		}

		try {
			if (!ssh.connect()) {
				logger.warn("Server username or password error!");
				return new BsmResult(false, "服务器地址端口、用户账号或密码错误");
			}
			String command = "dmidecode | grep UUID | awk '{print \"uuid:\"$2}';"
					+ "dmidecode -t memory | grep 'Type: D' | head -1 | awk '{print \"memoryMode:\"$2}';"
					+ "cat /proc/meminfo | grep MemTotal | awk '{print $2/1024}'| awk -F . '{if(substr($2,1,1)>5)\"memoryCapacity:\"$1+1;print \"memoryCapacity:\"$1}';"
					+ "cat /proc/cpuinfo |grep 'processor'|wc -l|awk '{print \"processorAmount:\"$1}';"
					+ "cat /proc/cpuinfo |grep 'vendor_id'|head -1|awk '{print \"processorVendor:\"$1}';"
					+ "cat /proc/cpuinfo |grep 'model name'|head -1|awk '{print \"processorMode:\"$4\" \"$5\" \"$6\" \"$7\" \"$8\" \"$9}';"
					+ "cat /etc/os-release | grep -iw name | awk -F = '{print $2}' | awk -F \\\" '{print \"osName:\"$2\" \"$3}';"
					+ "cat /etc/os-release | grep -iw version | awk -F = '{print $2}' | awk -F \\\" '{print \"osVersion:\"$2\" \"$3}';uname -a|awk '{print \"kernelVersion:\"$3}';"
					+ "fdisk -l | grep -i 'disk /dev/sda' | awk '{print $3}' | awk -F . '{if(substr($2,1,1)>5)\"diskCapacity:\"$1+1;print \"diskCapacity:\"$1}';"
					+ "dmidecode | grep -i -A6 'Bios Information' | grep -iw version | awk '{print \"biosVersion:\"$2}';"
					+ "dmidecode | grep -i -A6 'Bios Information' | grep -iw vendor | awk -F ': ' '{print \"biosVendor:\"$2}';"
					+ "dmidecode | grep -i -A6 'Bios Information' | grep -iw 'date' | awk -F ': ' '{print $2}' | awk -F '/' '{print \"biosDate:\"$3\"-\"$1\"-\"$2}';"
					+ "cat /etc/ssh/ssh_config | grep -iw port | awk '{print \"managerPort:\"$3}';"
					+ "cat /etc/sysconfig/network-scripts/ifcfg-eno1 | grep -iw ipaddr | awk -F = '{print \"managerIp:\"$2}';"
					+ "cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep -iw ipaddr | awk -F = '{print \"managerIp:\"$2}';"
					+ "docker info | grep -iw 'server version' | awk '{print \"dockerVersion:\"$3}';"
					+ "cat /proc/cpuinfo | grep vmx |head -1| awk '{if($1~/a\\s*b/)\"virtualized:false\";print \"virtualized:true\"}';"
					+ "cat /proc/cpuinfo | grep svm |head -1| awk '{if($1~/a\\s*b/)\"virtualized:false\";print \"virtualized:true\"}';"
					+ "cat /etc/sysconfig/network-scripts/ifcfg-eno1 | grep -i bootproto | awk -F = '{print \"protocal:\"$2}';"
					+ "cat /etc/sysconfig/network-scripts/ifcfg-eno1 | grep -i macaddr | awk -F = '{print \"mac:\"$2}';"
					+ "cat /etc/sysconfig/network-scripts/ifcfg-eno1 | grep -i dns1 | awk -F = '{print \"dns:\"$2}';"
					+ "cat /etc/sysconfig/network-scripts/ifcfg-eno1 | grep -i gateway | awk -F = '{print \"gateway:\"$2}';"
					+ "ifconfig eno1 | grep -iw netmask | awk '{print \"maskCode:\"$4}'";
			String result = ssh.executeWithResult(command);
			Server server = JSONObject.parseObject(ClassHelper.getJson(result).toJSONString(), Server.class);
			if (port != null)
				server.setManagerPort(port);
			if (ip != null)
				server.setManagerIp(ip);
			return new BsmResult(true, server, "获取服务器数据成功");
		} catch (Exception e) {
			logger.error("Connect ssh fail:", e);
			return new BsmResult(false, "服务器地址端口、用户账号或密码错误");
		} finally {
			ssh.close();
		}
	}

}
